<template>
  <div class="login-card">
    <div class="login-header">
      <h3>登录页面</h3>
      <p>欢迎回到 Code Studio</p>
    </div>

    <a-form
      :model="loginForm"
      :rules="loginRules"
      @finish="handleLogin"
      layout="vertical"
      class="login-form"
    >
      <a-form-item label="账号" name="username">
        <a-input
          v-model:value="loginForm.username"
          placeholder="请输入账号"
          size="large"
          class="form-input"
        >
          <template #prefix>
            <UserOutlined class="input-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item label="密码" name="password">
        <a-input-password
          v-model:value="loginForm.password"
          placeholder="请输入密码"
          size="large"
          class="form-input"
        >
          <template #prefix>
            <LockOutlined class="input-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <div class="switch-prompt-between">
        <span>还没有账户？</span>
        <a href="#" class="switch-link" @click.prevent="$emit('switchToRegister')">立即注册</a>
      </div>

      <a-form-item class="submit-btn-container">
        <a-button
          type="primary"
          html-type="submit"
          size="large"
          :loading="loginLoading"
          class="submit-btn"
          block
        >
          登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import axios from '../router/request.ts'

// 定义事件
const emit = defineEmits<{
  switchToRegister: []
  loginSuccess: [userInfo: any]
}>()

// 响应式数据
const loginLoading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, message: '密码不得少于8位', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  try {
    loginLoading.value = true

    // 后端接口调用（占位代码）
    const response = await axios.post('/user/login', {
      userAccount: loginForm.username,
      userPassword: loginForm.password
    })

    if (response.data.code === 0 && response.data.data) {
      message.success('登录成功！', 2)
      emit('loginSuccess', response.data)
    }
    else {
      message.error('登录失败，请检查账号密码')
    }

    // 清空表单
    Object.assign(loginForm, { username: '', password: '' })

  } catch (error) {
    console.error('登录失败:', error)
    message.error('登录失败!')
  } finally {
    loginLoading.value = false
  }
}

// 清空表单的方法
const clearForm = () => {
  Object.assign(loginForm, { username: '', password: '' })
}

// 暴露方法给父组件
defineExpose({
  clearForm
})
</script>

<style scoped>
.login-card {
  width: 100%;
  max-width: 400px;
  max-height: 480px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  padding: 32px;
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.1),
    0 2px 16px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05), rgba(118, 75, 162, 0.05));
  pointer-events: none;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
  position: relative;
  z-index: 1;
}

.login-header h3 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.login-form {
  position: relative;
  z-index: 1;
}

.form-input {
  margin-bottom: 10px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
}

.form-input:hover,
.form-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  background: rgba(255, 255, 255, 1);
}

.input-icon {
  color: #667eea;
}

.switch-prompt-between {
  text-align: right;
  margin-top: 24px;
  font-size: 14px;
  color: #666;
}

.switch-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  margin-left: 4px;
  transition: all 0.3s ease;
  position: relative;
}

.switch-link:hover {
  color: #764ba2;
  text-decoration: none;
}

.switch-link::after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: -2px;
  left: 0;
  background: linear-gradient(90deg, #667eea, #764ba2);
  transition: width 0.3s ease;
}

.switch-link:hover::after {
  width: 100%;
}

.submit-btn-container {
  margin-top: 24px;
  margin-bottom: 0;
}

.submit-btn {
  height: 48px;
  border-radius: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  font-weight: 600;
  font-size: 16px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.submit-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s;
}

.submit-btn:hover::before {
  left: 100%;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
  background: linear-gradient(135deg, #7c8cff 0%, #8a5fb8 100%);
}

/* 覆盖 Ant Design 样式 */
:deep(.ant-form-item-label > label) {
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

:deep(.ant-form-item) {
  margin-bottom: 20px;
}

:deep(.ant-form-item-explain-error) {
  font-size: 12px;
}
</style>
